package cn.kgc.config;

import cn.kgc.vo.PayVo;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@Slf4j
public class AlipayTemplate {
    // 商户appid
    public static String APPID = "2021000122602160";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCO2hGwFFQ48vBtNKIfKSqfRA2BObwFRXoAL+YV4RArhc5a0PIjl+FMhMJhXx/HWXrqbrvWhMmUCYsQNGAbEuEM8wcSAsfCkyx38QW/p6vukvVWbqAHvlFHgB0l/N+BipTOhZIQNZI7RrHj3tsqDlxQkXVBr+y6btilKtjhSOU4vlueLSaqNWH2PmOllWM+60wFnfrgjoSaL2GWQrFMLBq3m65zxa1oLn5NWIXqPKjVpl4nfg0/7wYVMuwZ1n1JDS/xBFkN+s3B9KiJueh3LDTY0medQap5thagHuDM8qE8f+2/t4aw4FsEx7coQaP6hXAZAj/f71rneoH26zXRueR/AgMBAAECggEAYdfLNac26Kq9Mo9SW7V+lAewmEpLFfE4Vth355R31teXN2zV7LsifZ6Q5NCam5gWMGwYDB53UwYWUcsN6H1N1+i9dwCgggz78EsNfPADOy09DS5IPgWE9kwxqstA6yFYOvwRxzhqKF+LiGAkDPI5GJYIki1FH4y8pqRqWCPtnKb6XWaExb/4Cn7qaSpkpto8pmT53vEATaj2jvhBePyk0EKkRsnZ/mQ/f6rw4YoiMJRtZRvEg0gYuLlRKACTxTnQD4RqhFZoeb9QYvZi8MmJ/j+I+hIczMX7FWIxOR+ItLn5wizl2b27tIpuUsttbnRRiXa4Z9D2fCxVGHg1ICIaQQKBgQDozAxsTnUeHgtIAYmMvBk7s22LLIpl2LY1KD0SBkyryo2KomXHPF0Bi/JYYyb6wO5dDGPRLmEjkbv+4JY8HmnBvgk7BpowA5bddogrRGFgny+Pw3rEUcLqm5dF5CyBZ4MjF8yQI4qoTp0O+HWwwI5pMf8wgh58/HsRNb2vb4db6wKBgQCdFwQb4S0XqnGtUeT6aRg26nmd5gzLCsjycPLeLqkbW/jOrJDw7+xToBRHR7HgqchNysvjSdQrasUd2h0aH9bRvpzMrP1a5xCVi1S+wR2fNeLb5vHiSMimi941a827tQjvuaC7Sxzo8kPTUV3PlTRNCkdCHARus4w7HkR85EcYvQKBgQC8kNObQy3wuEtRBxXArngN+LfJnMl518E3a6GUIRAnswlVe1fPu2BmqVIjMCVZxb8OFcQgo13BrwFFYeuvRL9LVMArVJy+OZoRRzrjOR9bYXXoEGI39TZaTK+KyFkYdJ0vZs6nDnL5A7yKbMz/3+k64mmfhDZn7SdOeGZuXXuaAwKBgE7um6/uixvTFt+BwM5aNEKTupwuVctIFgRL4JznUS1gHzqp1wJZa6PxUX8ndSdv6Ce+eIgyNpvCQF1plSnlSamGX38whPVCGUErC1/15Q50oxLfjjanX7rfmL2691sT8fOSkELL0TDzWkU6IysEy4zSV0/OiCOfsBycbslaYqb5AoGAAO4zs7nsPz+a4bxoOerAOQHj0VieIp2mh+MWtyGPBVRiuTcYHB8T/OuXxXLBatTbrYZYCc59svm5olWK7JD1RHsJm7XImHtmRHWFETrdnqKVciXdecGtgIvVq7mfbBNqWF2SuwjPq+0GQmseDXW6l/bT7u+1+o10TAU/whNvwi4=";

    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url="https://67go425789.zicp.fun/order/notifyUrl";

    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url="https://67go425789.zicp.fun/order/returnUrl";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjeCpGGJiUdsiFp1xRWhVp/N28+4pM1uM6xRcdQhkHf1HjniZO+VlPTTusdDQi+NO73xgwlIkw+dyrXje1HkeNgfDKB/IGBkfFPn2YqJC5ktyIN/pHWHEpKPyWM9CnItZH3MA3fIOsCsGbwEmyoYC77wVzOxQN0YHc0uGcrof2harfZLy07DJEq1cfMHynrdBFC9oSKvMdML2+Zeq/d3Hgl9eJvByNxVuEGYar8Pr/EZz/digNcALTFkC0bdK6E0TB6bZ+ll/gjev3NulB1BLecC0cgX7mVZlMy8wolbdZlBjgGZ3/vpATuHQ85uTcW1fyxbxaMVX8W5o8DHdbI6EJQIDAQAB";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    //调用支付方法  -->
    public String pay(PayVo payVo)throws AlipayApiException{
        //创建alipay支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(URL,
                APPID,RSA_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGNTYPE);

        //创建支付请求
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setReturnUrl(return_url);
        request.setNotifyUrl(notify_url);
       request.setBizContent(JSON.toJSONString(payVo));

       //支付宝客户端发送请求，获得支付宝返回的接口
       String result=alipayClient.pageExecute(request).getBody();
       log.info(result);
       return result;
    }
}